package com.uphone.magicleafteacher.pro.activity;

import android.arch.lifecycle.LifecycleOwner;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.radish.baselibrary.Intent.IntentData;
import com.radish.baselibrary.Intent.IntentUtils;
import com.radish.baselibrary.utils.LogUtils;
import com.radish.baselibrary.utils.SharedPreferencesHelper;
import com.radish.baselibrary.utils.ToastUtil;
import com.radish.framelibrary.view.glide.GlideUtils;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.FailureConsumer;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;
import com.uphone.magicleafteacher.util.AudioPlayProgressUtil;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.util.DateUtils;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.BaseGActivity;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.vo.AudioWorkDetailBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 说明：我的训练详情
 * 2019/11/5 17:26
 */
public class MytrainingdetailsActivity extends BaseGActivity {
    @BindView(R.id.iv_pic)
    ImageView ivPic;
    @BindView(R.id.iv_play)
    ImageView ivPlay;
    @BindView(R.id.sb_audio)
    SeekBar sbAudio;
    @BindView(R.id.tv_duration)
    TextView tvDuration;
    @BindView(R.id.title)
    TextView tv_title;
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.iv_switch)
    ImageView ivSwitch;
    @BindView(R.id.tv_phd)
    TextView tvPhd;
    @BindView(R.id.tv_zqx)
    TextView tvZqx;
    @BindView(R.id.tv_mfl)
    TextView tvMfl;
    @BindView(R.id.tv_lcd)
    TextView tvLcd;
    @BindView(R.id.tv_yqdj)
    TextView tvYqdj;
    @BindView(R.id.tv_jljf)
    TextView tvJljf;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    @IntentData
    private String id;
    @IntentData
    private String title;
    private AudioPlayProgressUtil audioPlayUtils;
    private String workBookName;
    private String charNum;
    private String time;
    private String audio;
    private String audioBook;
    private ArrayList<AudioWorkDetailBean.AudioWorkBean.ChaptersBean> chapterList;

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_training_details;
    }

    @Override
    protected void initView() {
        tv_title.setText(title);
    }

    @Override
    protected void initTitle() {
        audioPlayUtils = new AudioPlayProgressUtil();
    }

    @Override
    protected void initData() {
        processDetail();
    }

    @Override
    protected void initListener() {
        audioPlayUtils.setProgressListener(sbAudio, new AudioPlayProgressUtil.ProgressListener() {
            @Override
            public void start() {
                ivPlay.setSelected(true);
            }

            @Override
            public void progress(int progress, int currentDuration, int totalDuration) {
                if (progress != sbAudio.getProgress())
                    sbAudio.setProgress(progress);
                tvDuration.setText("" + DateUtils.getTimeFormLong(currentDuration));
            }

            @Override
            public void completion() {
                ivPlay.setSelected(false);
                tvDuration.setText(time);
            }

            @Override
            public void stop() {
                ivPlay.setSelected(false);
            }

            @Override
            public void fail() {
                ivPlay.setSelected(false);
            }
        });
    }

    @OnClick({R.id.iv_play, R.id.iv_switch, R.id.close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_play:
                if (view.isSelected()) {
                    audioPlayUtils.stop();
                } else {
                    audioPlayUtils.play(audio);
                }
                break;
            case R.id.iv_switch:
                IntentUtils.getInstance().with(this, PracticeDetailActivity.class)
                        .putString("audioPath", audioBook)
                        .putListSerializable("chapterList",chapterList)
                        .start();
                break;
            case R.id.close:
                finish();
                break;
        }
    }

    //我的训练详情
    private void processDetail() {
        SharedPreferences preferences = getSharedPreferences("moye", MODE_PRIVATE);
        String token = preferences.getString("token", "");
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("workId", id);
        MyApp.apiService(ApiService.class)
                .workDetail(map, map1)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    showLoading();
                })
                .doFinally(() -> {
                    closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) this)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        String str = jsonObject.toString();
                        LogUtils.e("===processDetail=" + str);
                        AudioWorkDetailBean bean = new Gson().fromJson(str, AudioWorkDetailBean.class);
                        if (bean.getCode() == 0) {
                            if (bean.getAudioWork() != null && bean.getAudioWork().getChapters() != null && bean.getAudioWork().getChapters().size() != 0) {
                                chapterList = (ArrayList<AudioWorkDetailBean.AudioWorkBean.ChaptersBean>) bean.getAudioWork().getChapters();
                                GlideUtils.getInstance().loadNormalImage(MytrainingdetailsActivity.this,
                                        CommonUtil.getImageUrl(chapterList.get(0).getChapterPic()), ivPic);
                            }
                            tvPhd.setText(bean.getAudioWork().getCoordinateScore() + "");
                            tvZqx.setText(bean.getAudioWork().getAccuracyScore() + "");
                            tvMfl.setText(bean.getAudioWork().getImitationScore() + "");
                            tvLcd.setText(bean.getAudioWork().getFluencyScore() + "");
                            tvYqdj.setText(bean.getAudioWork().getThoughtScore() + "");
                            tvJljf.setText(bean.getAudioWork().getJifen() + "");
                            tvRemark.setText(bean.getAudioWork().getRemark());
                            charNum = bean.getAudioWork().getCharNum();
                            workBookName = bean.getAudioWork().getWorkBookName();
                            audioBook = CommonUtil.getVideoUrl(bean.getAudioWork().getAudioPath());
                            audio = CommonUtil.getVideoUrl(bean.getAudioWork().getRemarkAudio());
                            AudioPlayProgressUtil.getDurationByUrl(audio, duration -> {
                                time = DateUtils.getTimeFormLong(duration);
                                tvDuration.setText(time);
                            });
                        } else {
                            ToastUtil.showShort(bean.getMsg());
                        }
                    }
                }, new FailureConsumer());
    }

}
